const vscode = require('vscode');
const axios = require('axios');

let token = '';

// 登录函数
async function login(username, password) {
    const url = `http://10.86.168.203:18889/user-server/user/login`;
    const headers = {
        'Content-Type': 'application/json',
        'product': 'dqdata'
    };
    const body = {
        loginName: username,
        loginType: 'password',
        password: password,
        productName: 'dqdata',
        sessionId: '',
        verifyCode: password
    };

    try {
        const response = await axios.post(url, body, { headers });
        if (response.data.errCode === 0) {
            token = response.data.data.token;
            vscode.window.showInformationMessage('登录成功！');
        } else {
            vscode.window.showErrorMessage(`登录失败：${response.data.errMsg}`);
        }
    } catch (error) {
        vscode.window.showErrorMessage('登录失败：' + (error.response && error.response.data ? error.response.data.message : error.message));
    }
}

// 激活函数
function activate(context) {
    // Hello World 命令
    const helloWorldDisposable = vscode.commands.registerCommand('dqjinrong.helloWorld', function () {
        vscode.window.showInformationMessage('Hello World from DQjinrong!');
    });

    // 登录命令
    const loginDisposable = vscode.commands.registerCommand('extension.login', async () => {
        const username = await vscode.window.showInputBox({ prompt: '输入用户名' });
        const password = await vscode.window.showInputBox({ prompt: '输入密码', password: true });
        if (username && password) {
            await login(username, password);
        }
    });

    // 将命令添加到上下文订阅中
    context.subscriptions.push(helloWorldDisposable, loginDisposable);

    console.log('Congratulations, your extension "dqjinrong" is now active!');
}

// 当插件停用时调用
function deactivate() {}

module.exports = {
    activate,
    deactivate
};